package org.fujay.commons.langchain4j.core.factory;

import org.fujay.commons.langchain4j.core.enumd.ModelSupplierEnum;
import org.fujay.commons.langchain4j.core.model.ModelProvider;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author slm
 * @description 模型工厂
 */
public class ModelFactory {

    private final Map<ModelSupplierEnum, ModelProvider> PROVIDER_MAP = new ConcurrentHashMap<>();

    @Autowired
    public ModelFactory(List<ModelProvider> providers) {
        this.PROVIDER_MAP.putAll(
                providers.stream().collect(Collectors.toMap(ModelProvider::modelSupplierEnum, Function.identity())));
    }

    public ModelProvider getModelProvider(ModelSupplierEnum modelSupplierEnum) {
        ModelProvider provider = PROVIDER_MAP.get(modelSupplierEnum);

        if (provider == null) {
            return PROVIDER_MAP.get(ModelSupplierEnum.DEFAULT_AI);
        }
        return provider;
    }
}
